package com.chen.integral.module.test.service.impl;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.springframework.http.ResponseEntity;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Title: manager-spring-mvc-hb
 * @Description: to do
 * @Author: chenjiwei
 * @Date: 2020-11-18 23:38
 * @Version: 1.0.0
 */
public class ExcelUtils {
    /**
     * @Author chenjiwei
     * @Description 导出
     * @Date 2020/11/19 1:30
     * @Param
     * @return
     */
    public ResponseEntity export(String fileName, String sheetName, List<Map<String, Object>> dataList) throws IOException {
        Map<String, Integer> titleMap = dataList.isEmpty() ? new HashMap<>() : getTitleMap(dataList.get(0));
        Workbook workbook  = new HSSFWorkbook();
        Sheet sheet = workbook.createSheet(sheetName);
        Row titlwRow = sheet.createRow(0);
        titlwRow.setHeightInPoints(30f);
        titleMap.forEach((k,v)->{
            sheet.setColumnWidth(v, k.length()*1024);
            Cell cell = titlwRow.createCell(v);
            cell.setCellValue(k);
            cell.setCellStyle(style(workbook));
        });

        int count = 1;
        for (int i = 0; i < dataList.size(); i++){
            Row row = sheet.createRow(i+1);
            row.setHeightInPoints(20f);
            dataList.get(i).forEach((k, v)->{
                Cell cell = row.createCell(titleMap.get(k));
                cell.setCellValue(v.toString());
                cell.setCellStyle(style(workbook));
            });
            count++;
        }

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        workbook.write(byteArrayOutputStream);
        String filename = new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
        return ResponseEntity
                .ok()
                .header("Content-Disposition", "attachment; filename="+filename+".xls")
                .body(byteArrayOutputStream.toByteArray());
    }

    /**
     * @Author chenjiwei
     * @Description 转换并获取表头map
     * @Date 2020/11/19 1:30
     * @Param
     * @return
     */
    public Map<String, Integer> getTitleMap(Map<String, Object> map){
        int index = 0;
        Map<String,Integer> innerMap = new HashMap<>();
        for( String key : map.keySet()){
            innerMap.put(key, index);
            index++;
        }
        return innerMap;
    }

    /**
     * @Author chenjiwei
     * @Description 基础样式
     * @Date 2020/11/19 1:30
     * @Param
     * @return
     */
    public CellStyle style(Workbook workbook){
        Font font = workbook.createFont();
        font.setFontHeightInPoints((short) 13);
        font.setColor(Font.COLOR_NORMAL);
        font.setBold(true);
//        font.setBoldweight(Font.BOLDWEIGHT_BOLD);                       //加粗
        font.setFontName("微软雅黑");


        //标题行的style
        CellStyle titleCellStyle = workbook.createCellStyle();
        // 注释掉的是不能用的
        // Require Apache POI version at least 4.1.1, but now is 3.12, please check the dependency of project.
//        titleCellStyle.setAlignment(CellStyle.ALIGN_CENTER);                //居中
//        titleCellStyle.setWrapText(true);                                   //自动换行
//        titleCellStyle.setVerticalAlignment(CellStyle.ALIGN_CENTER);

        titleCellStyle.setFont(font);

        return titleCellStyle;
    }
}
